<?php
/**
 * Date: 2017/11/10
 * Time: 10:08
 * Author: zeronlee
 */

namespace App\Http\Controllers\Backend\Distribute;


use App\Http\Controllers\Controller;
use App\Models\Access\User;
use App\Models\Announce\Announce;
use App\Models\Flaw\Vuls;
use App\Models\Ogz;
use App\Models\App;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Log;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use  Illuminate\Database\Query\Builder;


class DistributeController extends Controller
{

    /**我分发的漏洞
     * @param Request $request
     * @return $this
     */
    public function index(Request $request)
    {
        $user_id = Auth::id();
        $fields=[];
        $record= DB::table('distribute')
            ->where('handle', '=', $user_id)
            ->select("vuls_id",DB::raw('max(id) as id'))
            ->groupBy('vuls_id')
            ->get()
            ->toArray();
        if($record){
            foreach ($record as $value){
                $vuls[]=$value->id;
            }
            $fields = DB::table('distribute')
                ->join('vuls', 'vuls.id', '=', 'distribute.vuls_id')
                ->leftJoin('app', 'app.id', '=', 'distribute.app_id')
                ->leftJoin('level', 'level.id', '=', 'vuls.level_id')
                ->leftJoin('category', 'category.id', '=', 'vuls.category_id')
                ->whereIn('distribute.id',$vuls)
                ->select("distribute.*", "vuls.vuls_number", "vuls.title", "vuls.description", "vuls.status", "app.name as appname", 'level.name as lname', 'category.name as cname')
                ->paginate(10);
        }


        return view('backend.distribute.index',
            [
                'lists' => $fields,
                'vulStatus' => Vuls::$vulsStatus,
            ]);
    }


    /**我收到的分发漏洞信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function recived(Request $request)
    {

        $user_id = Auth::id();
        $fields = DB::table('distribute')
            ->join('vuls', 'vuls.id', '=', 'distribute.vuls_id')
            ->leftJoin('app', 'app.id', '=', 'distribute.app_id')
            ->leftJoin('level', 'level.id', '=', 'vuls.level_id')
            ->leftJoin('category', 'category.id', '=', 'vuls.category_id')
            ->where('distribute.users_id', '=', $user_id)
            ->select("distribute.*", "vuls.vuls_number", "vuls.title", "vuls.description", "vuls.status", "app.name as appname", 'level.name as lname', 'category.name as cname')
            ->paginate(10);


        return view('backend.distribute.recived', [
            'lists' => $fields,
            'vulStatus' => Vuls::$vulsStatus,
        ]);

    }

    /**漏洞详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
     */
    public function det(Request $request)
    {
        $id = intval($request->id);//漏洞ID
        if ($id) {
            $vuls = DB::table('vuls')
                ->join('level', 'level.id', '=', 'vuls.level_id')
                ->leftJoin('users', 'users.id', '=', 'vuls.user_id')
                ->leftJoin('category', 'category.id', '=', 'vuls.category_id')
                ->leftJoin('app', 'app.id', '=', 'vuls.app_id')
                ->where('vuls.id', '=', $id)
                ->select('vuls.id', 'vuls.status', 'vuls.level_id', 'vuls.title', 'vuls.content', 'vuls.description', 'vuls.app_id', 'vuls.vuls_number', 'vuls.category_id', 'users.source', 'vuls.form_url', 'vuls.payload', 'vuls.url_parms',
                    'category.name as cname', 'level.name as  lname', "app.name as appname")
                ->first();
            return view('backend.distribute.det', [
                    'vuls' => $vuls,
                ]
            );
        } else {
            return abort(404);
        }
    }

}